#include "logdialog.h"
#include "ui_logdialog.h"

#include <QMessageBox>
#include <QSqlQuery>
#include <QDebug>
logDialog::logDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::logDialog)
{
    ui->setupUi(this);
    ui->edit_password->setEchoMode(QLineEdit::Password);
    this->setWindowTitle("用户登陆");
}

logDialog::~logDialog()
{
    delete ui;
}

void logDialog::getInfo(QString &v1, QString &v2, int &v3)
{
   v1 =  account;
   v2 =  password;
   v3 =  usertype;
}

void logDialog::on_pushButton_clicked()
{
    if(ui->edit_accout->text().isEmpty())
    {
        QMessageBox::warning(this,QString("错误")
                              ,QString("请输入账号信息"));
        return ;
    }
    else if(ui->edit_password->text().isEmpty())
    {
        QMessageBox::warning(this,QString("错误")
                              ,QString("请输入密码"));
        return ;
    }
    QSqlQuery query;
    bool ans = query.exec(QString("select *from User where UserName = '%1'").arg(ui->edit_accout->text()));
    qDebug()<<query.lastQuery();
    if(true ==  ans)
    {
        if(!query.next())
        {
            QMessageBox::warning(this,QString("错误")
                                  ,QString("没有这个账户"));
            return ;
        }
        account = query.value(0).toString();
        password = query.value(1).toString();
        usertype = query.value(2).toInt();
        if(account == ui->edit_accout->text() && password == ui->edit_password->text())
        {
              this->accept();
        }
        else
        {
            QMessageBox::warning(this,QString("错误")
                                  ,QString("用户或密码错误"));
        }
    }
    else
    {
        QMessageBox::warning(this,QString("错误")
                              ,QString("查询失败"));
    }

}

void logDialog::on_pushButton_2_clicked()
{
    exit(0);
}
